package com.bbcare.core.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletInputStream;

/**
 * 
 * @name:         PostServletInputStream.java
 * @author:       tao
 * @createTime:   2018年3月12日
 * @description:  PostServletInputStream.java
 * @modify:
 *
 */
public class PostServletInputStream extends ServletInputStream {
	private InputStream inputStream;
    private String body ;//解析json之后的文本
    private String enc;
 
    public PostServletInputStream(String body,String enc) throws IOException {
        this.body=body;
        this.enc = enc;
        inputStream = null;
       
    }
 
 
    private InputStream acquireInputStream() throws IOException {
        if(inputStream == null) {
            inputStream = new ByteArrayInputStream(body.getBytes(enc));//通过解析之后传入的文本生成inputStream以便后面control调用
        }
 
        return inputStream;
    }
 
 
    public void close() throws IOException {
        try {
            if(inputStream != null) {
                inputStream.close();
            }
        }
        catch(IOException e) {
            throw e;
        }
        finally {
            inputStream = null;
        }
    }
 
 
    public int read() throws IOException {
        return acquireInputStream().read();
    }
 
 
    public boolean markSupported() {
        return false;
    }
 
 
    public synchronized void mark(int i) {
        throw new UnsupportedOperationException("mark not supported");
    }
 
 
    public synchronized void reset() throws IOException {
        throw new IOException(new UnsupportedOperationException("reset not supported"));
    }
}
